package com.phil.philreports.asynccelltablesearch.server.jdo;

import java.util.ArrayList;
import java.util.Random;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable
public class Person {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	
	@Persistent
	private String firstname;
	
	@Persistent
	private String lastname;
	
	@Persistent
	private String email;
	
	public Person(String firstname, String lastname, String email) {
		this.firstname = firstname;
		this.lastname = lastname;
		this.email = email;
	}
	
	public Key getKey() {
		return this.key;
	}
	
	public String getFirstname() {
		return this.firstname;
	}
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}
	
	public String getLastname() {
		return this.lastname;
	}
	public void setLastname(String lastname) {
		this.lastname = lastname;
	}
	
	public String getEmail() {
		return this.email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
	public static void createTestData(int count) {
		ArrayList<Person> persons = new ArrayList<Person>();
		
		int noToGenerate = count;
		int firstNameLength = 3;
		int lastNameLength = 1;
		String alphabet = "abcdefghijklmnopqrstuvwxyz";
		Random random = new Random();
		
		for(int i = 0; i < noToGenerate; i++) {
			String firstName = ""+i;
			String lastName = "";
			for(int j = 0; j < firstNameLength; j++) {
				firstName += ""+alphabet.charAt(random.nextInt(alphabet.length()));
			}
			for(int j = 0; j < lastNameLength; j++) {
				lastName += ""+alphabet.charAt(random.nextInt(alphabet.length()));
			}
			String email = "";
			email += ""+alphabet.charAt(random.nextInt(alphabet.length()));
			email += "@";
			email += ""+alphabet.charAt(random.nextInt(alphabet.length()));
			email += ".";
			email += ""+alphabet.charAt(random.nextInt(alphabet.length()));
		
			persons.add(new Person(firstName, lastName, email));
			
		}
		
		// Insert into database
		PersistenceManager pm = PMF.get().getPersistenceManager();
		pm.makePersistentAll(persons);
		pm.close();
	}
	
}
